<?php
// 本类由系统自动生成，仅供测试用途
class UserAction extends Action {
    //首页
	public function index()
	{
		$this->checkLogin();
		if(!IS_LOGIN) $this->redirect('User/login');
		$this->redirect('Index/index');
	}
    
    
    //登录页面
    public function login(){
    	$this->checkLogin();
    	if(IS_LOGIN) $this->redirect('User/index');
    	else if(strtolower($_SERVER['REQUEST_METHOD']) == 'post')
    	{
    		$cond = $_POST;
    		if($_SESSION['verify'] != md5($cond['verify'])) {
    			if($cond['username'] == "" || $cond['password'] == "" ){
    				$this->assign('erorsta','1');
					$this->assign('error','登录失败：用户名或密码不能为空');
    			}else{
					$this->assign('erorsta','1');
					$this->assign('error','登录失败：验证码错误');
    			}
    		}else{
				
				$rs = $this->keeplogin($cond);
				if($rs == '2'){
					$this->assign('erorsta','1');
					$this->assign('error','登录失败：该用户未激活');
				}else if($rs == '3'){
					$this->assign('erorsta','1');
					$this->assign('error','登录失败：该用户已被禁用');
				}
				else if($rs == '1')
				{
					if(session(C('ADMIN_AUTH_KEY'))){
						$this->redirect('Admin/index');
					}else{
						if($_GET['type'] && $_GET['vid']){
							$model = $_GET['type']."/info";
							$this->redirect($model,array('vid'=>$_GET['vid']));
						}else{
							$this->redirect('Index/index');
						}
					}
				}else{
					$this->assign('erorsta','1');
					$this->assign('error','登录失败：帐号或者密码错误');
				}
				
			}
    	}
    	$map['status'] = 1;
    	$artlist = M('Confart')->field('arid,artitle')->where($map)->select();
		$artnum = count($artlist);
		$this->assign('artnum',$artnum);
		$this->assign('artlist',$artlist);
    	$this->assign('type',$_GET['type']);
    	$this->assign('vid',$_GET['vid']);
    	$this->assign('_title_','用户登录');
		$this->display();
    }
    
    //退出登录
    public function logout()
    {
    	$this->checkLogin();
		if(!IS_LOGIN) $this->redirect('User/login');
    	if(session(C("USER_AUTH_KEY")) || session(C("ADMIN_AUTH_KEY")))
		{
			if(session(C("USER_AUTH_KEY"))){
				session(C("USER_AUTH_KEY"),NULL);	
			}
			if(session(C("ADMIN_AUTH_KEY"))){
				session(C("ADMIN_AUTH_KEY"),NULL);
			}
			cookie(C('USER_AUTH_ACCOUNT'),null,-3600);
			cookie(C('USER_AUTH_PWD'),null,-3600);
		}
    	$this->redirect('Index/index');
    }
    
    
    //注册页面
    public function reg()
    {
    	$this->checkLogin();
		if(IS_LOGIN) $this->redirect('User/index');
		if(strtolower($_SERVER['REQUEST_METHOD']) == 'post')
		{
			
			$cond = $_POST;
			//$cond['password']=md5($cond['password']);
			$cond['_islogin'] = true;
			$cond['_model'] = 'User';
			$cond['email'] = $_POST['email']."@".$_POST['emtype'];
			$cond['birth'] = $_POST['year']."年".$_POST['moth']."月".$_POST['day']."日";
			$cond['_column'] = array('username','password','repassword','nickname','sex','email','birth','address','tel');
			$title = "中国气象资源用户激活";
			$content = "尊敬的".$cond['username']."用户你好,点击下面链接进行激活:http://ssdrtx.gicp.net:10080/develop/index.php/user/jihuo/email/".$cond['email'];
			//echo $cond['email']."|";
			//echo $title."|";
			//echo $content."|";
			//print_r(SendMail($cond['email'],$title,$content));
			if(SendMail($cond['email'],$title,$content))
			{
				$rs = $this->register($cond);
				if($rs['rs'] == 'ok')
				{
					$this->redirect('User/regthr');
				}
				else{
					$this->assign('erorsta','1');
					$this->assign('error',$rs['error']);
					echo "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" /></head>";
					echo $rs['error'];
					exit;
				}
			}else{
				$this->redirect('User/reg',array('zcstas'=>1));
			}
		}
		if($_GET['zcstas'] == 1){
			$this->assign('tssta',"1");
			$this->assign('tsinfo',"注册失败！");
		}
		$map['status'] = 1;
    	$artlist = M('Confart')->field('arid,artitle')->where($map)->select();
		$artnum = count($artlist);
		$this->assign('artnum',$artnum);
		$this->assign('artlist',$artlist);
		$this->assign('_title_','注册用户');
		$this->display();
    }
    
    //提示页面
    public function regthr(){
    	$map['status'] = 1;
    	$artlist = M('Confart')->field('arid,artitle')->where($map)->select();
		$artnum = count($artlist);
		$this->assign('artnum',$artnum);
		$this->assign('artlist',$artlist);
    	$this->display();
    }
    
    //激活用户
    public function jihuo(){
    	$confinfo = M('Config')->find();
		$map1['status'] = 1;
		$artlist = M('Confart')->field('arid,artitle')->where($map1)->select();
		$artnum = count($artlist);
		$this->assign('artnum',$artnum);
		$this->assign('artlist',$artlist);
		$this->assign("_basetitle_",$confinfo["title"]);
		$this->assign("_desc_",$confinfo["content"]);
		$this->assign("_key_",$confinfo["sitekey"]);
		//获取搜索的分类
		$category = M('Category');
		$map1['cateid']=array('LT',10);
		$sclist=$category->where($map1)->order('cateid asc')->select();
		$this->assign("sclist",$sclist);
		//获取热门关键字
		$keyword = M('Keyword');	
		$kap['category']=0;	
		$keyArr = $keyword->where($kap)->order("orders asc")->select();
		$this->assign('keyArr',$keyArr);
		$this->assign('_title_','用户激活');
    	if($_GET['email']){
    		$map['email'] = $_GET['email'];
    		$data['status'] = 1;
    		$re = M('User')->where($map)->save($data);
    		if($re){
    			$this->assign('info','激活成功');
    		}else{
    			$this->assign('info','激活失败');
    		}
    	}else{
    		$this->assign('info','激活失败');
    	}
    	$this->display();
    }
        
    //个人空间
    public function space()
    {
    	echo '登录成功';
    }
    
    //检查是否登录
	public function checkLogin()
	{
		if(session(C("USER_AUTH_KEY")) || session(C("ADMIN_AUTH_KEY"))) define("IS_LOGIN",true);
		elseif(cookie(C('USER_AUTH_ACCOUNT')) && cookie(C('USER_AUTH_PWD')))
		{
			$cond['_column']['username'] = cookie(C('USER_AUTH_ACCOUNT'));
			$cond['_column']['password'] = cookie(C('USER_AUTH_PWD'));
			if(UserAction::keeplogin($cond)== '1') define("IS_LOGIN",true);
			else
			{
				cookie(C('USER_AUTH_PWD'),null,-3600);
				define("IS_LOGIN",false);
			}
		}
		else define("IS_LOGIN",false);
		return IS_LOGIN;
	}
	
	//保存登录信息
	public function keeplogin($cond)
	{
		if(is_array($cond))
		{
			$model = $cond['_column']['model'];
			$col_a = $cond['_column']['username'];
			$col_p = $cond['_column']['password'];

			if(!$model) $model = "User";
			if(!$col_a) $col_a = "username";
			if(!$col_p) $col_p = "password";
			$map[$col_a] = $cond[$col_a];
			$map[$col_p] = md5($cond[$col_p]);

			$rs = M($model)->where($map)->find();
/*			$sqlstring = M($model)->getLastSql();
			echo $sqlstring."<br/>";
			echo "<pre>";
			print_r($rs);
			exit;*/
			if($rs){
				if($rs['status'] == '1')
				{
					cookie(C('USER_AUTH_ACCOUNT'),$rs["username"],3600*24*30);
					cookie(C('USER_AUTH_PWD'),$rs["password"],3600*24*30);
					if($rs["utype"] == 0){
						$rs = serialize($rs);//序列化数组
						session(C('USER_AUTH_KEY'),$rs);
						return 1;
					}else{
						$rs = serialize($rs);//序列化数组
						session(C('ADMIN_AUTH_KEY'),$rs);
						return 1;
					}
				}else if($rs['status'] == '2'){
					return 3;
				}else{
					return 2;
				}
			}else{
				return FALSE;
			}
		}
		return false;
	}
	
	//保存注册信息
	public function register($cond)
	{
		if(isset($cond['_model'])) $model = $cond['_model'];
		else $model = "User";

		foreach ($cond['_column'] as $one) $data[$one] = trim($cond[$one]);
		
		$model = D($model);
		if($model->create($data))
		{
			//print_r($model->getLastSql());
			if(($uid = $model->add()) != false)
			{
				$map['uid']=$uid;
				$map['score']=100;
				$model->save($map);
				$rs['rs'] = 'ok';
				$rs['uid'] = $uid;
				
				//如果注册后需要直接登录
				/*if(isset($cond['_islogin']) && $cond['_islogin'])
				{
					$login = $_POST;
					if(isset($cond['_login'])) $login['_column'] = $cond['_login'];
					$this->keeplogin($login);
				}*/
			}
			else
			{
				$rs['rs'] = 'fail';
				$rs['error'] = '数据异常，请联系管理员';
			}
		}
		else
		{
			$rs['rs'] = 'fail';
			$rs['error'] = $model->getError();
		}
		return $rs;
	}
	
	//检查用户名是否存在
	public function checkuname()
	{
		$username = $_POST['username'];
		$map['username'] = $username;
		$res = M('User')->where($map)->find();
		echo $res;
	}
	
	//验证邮件格式
	public function checkemail()
	{
		if($_POST['email']){
			$email = $_POST['email'];
			$type = $_POST['emtype'];
			$email = $email."@".$type;
			$map['email'] = $email;
			$res = M('User')->where($map)->find();
			echo $res;
		}else{
			
			return false;
		}	
	}
	
	
	
	//验证码
	public function verify(){
    	import("ORG.Util.Image");	
		Image::buildImageVerify();
	}
	
	/**
	 * 跳转到找回密码页面
	 * @author 沈威
	 * @tel 15005164276
	 * @email 403382132@qq.com
	 */
	public function findpassword()
	{
		$confinfo = M('Config')->find();
		$map['status'] = 1;
		$artlist = M('Confart')->field('arid,artitle')->where($map)->select();
		$artnum = count($artlist);
		$this->assign('artnum',$artnum);
		$this->assign('artlist',$artlist);
		$this->assign("_basetitle_",$confinfo["title"]);
		$this->assign("_desc_",$confinfo["content"]);
		$this->assign("_key_",$confinfo["sitekey"]);
		//获取搜索的分类
		$category = M('Category');
		$map['cateid']=array('LT',10);
		$sclist=$category->where($map)->order('cateid asc')->select();
		$this->assign("sclist",$sclist);
		//获取热门关键字
		$keyword = M('Keyword');	
		$kap['category']=0;	
		$keyArr = $keyword->where($kap)->order("orders asc")->select();
		$this->assign('keyArr',$keyArr);
		$this->assign('_title_','找回密码');
		$this->display();
	}
	
	/**
	 * 找回密码
	 * Enter description here ...
	 */
	public function dofindpassword()
	{
		$cond = $_POST;
		if($_SESSION['verify'] != md5($cond['verify']))
		{
			$this->error('登录失败：验证码错误');
		}
		else
		{
			$map['username'] = $cond['username'];
			$map['email'] = $cond['email'];
			$usermodel=M('User');
			$user = $usermodel->where($map)->find();
			if($user)
			{
				$rodom_password=rand(100000,999999);
				$md5_password=md5($rodom_password);
				$title_find="中国气象资源用户找回密码";
				$url=U('User/login');
				$content_find="您好，系统已将您的密码修改为:".$rodom_password.",请勿泄露此密码,如需修改密码可登陆网站后修改!感谢您的支持!点击此链接登陆中国气象资源网:http://ssdrtx.gicp.net:10080".$url;
				if(SendMail($cond['email'],$title_find,$content_find))
				{
					$user['password']=$md5_password;
					if($usermodel->save($user))
					{
						$this->success("申请成功,邮件已发送!",U('User/login'));
					}
					else{
						$this->error('数据操作失败!');
					}
				}else{
					$this->error('邮件发送失败!');
				}
				
				echo $rodom;
			}
			else
			{
				$this->error('未找到此用户!请输入正确的用户名和邮箱');
			}
		}
	}
	

}